<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        //链接数据库 M();  取出所有数据  使用find()方法
        $user = M('user')->find($_SESSION['id']);   //通过id找到对应的用户信息
        $this->assign('user',$user); 

        // 处理分页
        // 关键变量：
        // 1.每页几条
        $page_num = 2;
        // 2.当前是第几页？
        if($_GET['page']>0){
        	$page = $_GET['page'];
        }
        else{
        	$page = 1;
        }

        // 组合分段条件
        $start = ($page-1)*$page_num;
        $end = $page_num;
        // 0,2  前面0表示起始位置，2表示取得条数
        $limit = $start.','.$end;

        // 求出总行数
        $sql = "SELECT COUNT(*) AS rows #mysql 求出某个表的总数据行数
        				FROM question ";
        $pp = M()->query($sql);	
        $rows = $pp[0]['rows'];

        // 根据总行数求出总页数
        $page_count = ceil($rows/$page_num);
        // echo $page_count;
        $this->assign('page_count',$page_count);

        // print_r($page_count);

        $questions = "SELECT t1.id AS uid,t1.nickname,t2.title,t2.create_time,t1.face,t2.id AS qid,t2.answer_num,t2.view_num
        			        FROM user t1 
        			        inner join question t2 
        			        on t1.id=t2.uid    
        			        ORDER BY t2.create_time DESC  #排序 
							LIMIT  $limit  #取0到第二条
        			        ";
        // echo $questions;
        // die;			        
        $question = M()->query($questions);

        $this->assign('question',$question);

        //近一月回答榜-TOP12
        $sql = "SELECT answer_num,face,nickname,id FROM user  ORDER BY answer_num DESC";
        $ans_num = M()->query($sql);
        $this->assign('ans_num',$ans_num);
        //var_dump($ans_num);
       

        //最近热帖
        $hots = "SELECT title,view_num,id FROM question 
        				ORDER BY view_num DESC,   #首要条件
								  answer_num DESC  #次要条件
        			";
        $hot =  M()->query($hots);
        $this->assign('hot',$hot);

        //最近热议
        $talks = "SELECT title,answer_num,id FROM question ORDER BY answer_num DESC,view_num DESC";
        $talk=  M()->query($talks);
        $this->assign('talk',$talk);
        

        $this->display();
    }


    //搜索
    public function search(){
        $user = M('user')->find($_SESSION['id']);  
        $this->assign('user',$user);

        // 搜索关键字
        if(!empty($_GET['word'])){
            $word = $_GET['word'];
            // 根据关键字去数据库查找数据
            // 使用模糊查询
            $sql = "SELECT q.answer_num,q.view_num,q.create_time,q.id,q.title,q.content,  #问题的标题，内容
                           u.nickname,u.id,u.face   #用户信息
                         FROM question AS q,user AS u
                    WHERE 
                    u.id = q.uid AND #限定连表查询条件
                    (q.title LIKE '%$word%' OR q.content LIKE '%$word%')
            ";
            // 这里只查找到问题
            $questions = M()->query($sql);
            //var_dump($questions);
            // 还要去答案表里查询
            // 使用模糊查询
            $sql = "SELECT q.create_time,q.id,q.content,q.qid,  #问题的标题，内容
                           u.nickname,u.id,u.face   #用户信息
                         FROM answer AS q,user AS u
                    WHERE 
                    u.id = q.uid AND #限定连表查询条件
                    ( q.content LIKE '%$word%')
            ";

            // 这里只查找答案
            $answer = M()->query($sql);
            
            //注入模板
            $this->assign('question',$questions);
            $this->assign('answer',$answer);

        }



        $this->display();
    }
}